//index.js
const app = getApp();

Page({
  data: {
    shopList: [
      {
        id: "sdameoefklekfeklf",
        coverPicture: "",
        shopName: "石家庄最强装修",
        shopIntro: "这是第一个店铺",
        collection: false,
        scoreTotal: 0
      },
      {
        id: "fwooo454",
        coverPicture: "",
        shopName: "超级马里奥",
        shopIntro: "这是第二个店铺",
        collection: true,
        scoreTotal: 10
      },
    ]
  },

  onLoad: function() {
    if (!wx.cloud) {
      wx.redirectTo({
        url: "../chooseLib/chooseLib"
      });
      return;
    }

    // 获取用户信息
    wx.getSetting({
      success: res => {
        if (res.authSetting["scope.userInfo"]) {
          // 已经授权，可以直接调用 getUserInfo 获取头像昵称，不会弹框
          wx.getUserInfo({
            success: res => {
              this.setData({
                avatarUrl: res.userInfo.avatarUrl,
                userInfo: res.userInfo
              });
            }
          });
        }
      }
    });
  },
  onSuccess: function(res) {
    //onSuccess回调
    wx.hideLoading();
    console.log(res);

    if (res.stateCode != 100) {
      console.log(res.message);
      this.show({
        iconToast: "", // 对：icon-dui, 错：icon-cuo,警告：icon-warning
        imageToast: "",
        textToast: res.message,
        duration: 100
      });
      if (res.stateCode == 110) {
        wx.redirectTo({
          url: "../phone/phone"
        });
      }
    } else {
      this.setData({
        items: res.data.ActivityProduct, //请求结果数据
        activity: res.data.activity,
        points: res.data.Points
      });
    }
  },
  previewAction(item) {
    // wx.navigateTo({
    //   url: "../shopDetail/shopDetail"
    // });
  },
  loveAction(){
    this.data.collection = !this.data.collection;

  },
  onGetUserInfo: function(e) {
    if (!this.data.logged && e.detail.userInfo) {
      this.setData({
        logged: true,
        avatarUrl: e.detail.userInfo.avatarUrl,
        userInfo: e.detail.userInfo
      });
    }
  },

  onGetOpenid: function() {
    // 调用云函数
    wx.cloud.callFunction({
      name: "login",
      data: {},
      success: res => {
        console.log("[云函数] [login] user openid: ", res.result.openid);
        app.globalData.openid = res.result.openid;
        wx.navigateTo({
          url: "../userConsole/userConsole"
        });
      },
      fail: err => {
        console.error("[云函数] [login] 调用失败", err);
        wx.navigateTo({
          url: "../deployFunctions/deployFunctions"
        });
      }
    });
  },

  // 上传图片
  doUpload: function() {
    // 选择图片
    wx.chooseImage({
      count: 1,
      sizeType: ["compressed"],
      sourceType: ["album", "camera"],
      success: function(res) {
        wx.showLoading({
          title: "上传中"
        });

        const filePath = res.tempFilePaths[0];

        // 上传图片
        const cloudPath = "my-image" + filePath.match(/\.[^.]+?$/)[0];
        wx.cloud.uploadFile({
          cloudPath,
          filePath,
          success: res => {
            console.log("[上传文件] 成功：", res);

            app.globalData.fileID = res.fileID;
            app.globalData.cloudPath = cloudPath;
            app.globalData.imagePath = filePath;

            wx.navigateTo({
              url: "../storageConsole/storageConsole"
            });
          },
          fail: e => {
            console.error("[上传文件] 失败：", e);
            wx.showToast({
              icon: "none",
              title: "上传失败"
            });
          },
          complete: () => {
            wx.hideLoading();
          }
        });
      },
      fail: e => {
        console.error(e);
      }
    });
  }
});
